<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class School extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'passport/index' );
			exit ();
		}
	}
	/*
	 * 返回学校列表
	 */
	public function index() {

		$this->load->library ( 'pagination' );
		// 每页显示数据条数
		$per_page=10;//暂定为10条

		$config['base_url']=site_url('school/index');
		$this->load->model ( 'school_model' );

		// 数据总数
		$config ['total_rows'] = $this->school_model->count_schools();
		//$config ['total_rows']=200;
		$config ['per_page'] = $per_page;
		$config ['first_link'] = '第一页';
		$config ['last_link'] = '最后一页';
		$config ['next_link'] = '下一页';
		$config ['prev_link'] = '上一页';
		$config['uri_segment'] =3;

		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		//偏移量
		$offset=intval($this->uri->segment(3));
		$data ['schools']=$this->school_model->get_schools ($per_page,$offset);
		$data ['title'] = '学校列表 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'school/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 跳转到添加学校页面
	 */
	public function add() {
		$data ['title'] = '添加学校 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'school/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 跳转到编辑学校页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'school_model' );
		$data ['school'] = $this->school_model->load ( $id );
		$data ['title'] = '编辑学校 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'school/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存学校信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$newschool = array (
				'name' => $this->input->post ( 'name' ),
				'type' => $this->input->post ( 'type' )
		);
		$this->load->model ( 'school_model' );
		if ($id) {
			if($this->school_model->update ( $id, $newschool )){
				return_json(TRUE,'','修改成功！');
			}else{
				return_json(FALSE,'','信息未改动！');
			}
		} else {
			$schoolid=$this->school_model->create ( $newschool );
			if(!empty($schoolid)){
				return_json(TRUE,'','添加成功！');
			}else{
				return_json(FALSE,'','添加失败！');
			}
		}
	}
	/*
	 * 删除学校信息
	 */
	public function delete() {
	    $id = $this->input->post('id');
		$this->load->model ( 'school_model' );
		$num = $this->school_model->count_class_by_school ( $id );
		if ($num>0) {
			return_json(FALSE,'','该学校下还有班级，请先删除该校班级！');
		}
		if ($this->school_model->delete ( $id )) {
			return_json(TRUE,'','删除成功！');
	    }else{
	    	return_json(FALSE,'','删除失败！');
	    }
    }
	/*
	 * 跳转到班级管理页面
	 */
	public function classindex() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'school_model' );
		$school= $this->school_model->load ( $id );
		$grades= $this->school_model->find_grade_by_type ( $school['type'] );
		$data ['id']=$id;
		//$data ['school']=$school;
		$data ['grades']=$grades;
		$this->load->library ( 'pagination' );
		// 每页显示数据条数
		$per_page=10;//暂定为10条

		$config['base_url']=site_url('school/classindex/id/'.$id);//
		$this->load->model ( 'school_model' );

		// 数据总数
		$config ['total_rows'] = $this->school_model->count_class_by_school($id);
		//$config ['total_rows']=200;
		$config ['per_page'] = $per_page;
		$config ['first_link'] = '第一页';
		$config ['last_link'] = '最后一页';
		$config ['next_link'] = '下一页';
		$config ['prev_link'] = '上一页';
		$config['uri_segment'] =5;

		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		//偏移量
		$offset=intval($this->uri->segment(5));
		$data ['classes']=$this->school_model->find_class_by_school ($id,$per_page,$offset);
		$data ['title'] = '班级管理 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'school/classindex' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 添加班级信息
	 */
	public function addclass() {
		$school = $this->input->post ( 'school' );
		$grade = $this->input->post ( 'grade' );
		$num = $this->input->post ( 'num' );
		$this->load->model ( 'school_model' );
		for($i=1;$i<=$num;$i++){
			$name=$i.'班';
			$class = array (
				'schoolid' => $school,
				'gradeid' => $grade,
				'name' => $name,
			);
			$this->school_model->createclass ( $class )	;
		}
		return_json ( TRUE, '', '添加成功！' );
	}
	/*
	 * 删除班级信息
	 */
	public function deleteclass() {
	    $id = $this->input->post('id');
		$this->load->model ( 'school_model' );
		if ($this->school_model->deleteclass ( $id )) {
			return_json(TRUE,'','删除成功！');
	    }else{
	    	return_json(FALSE,'','删除失败！');
	    }
    }
	/*
	 * 返回菜谱页面
	 */
	public function chooseproduct() {
		$id = $this->input->post('id');
		$data ['title'] = '每周菜单 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'school/chooseproduct' );
		$this->load->view ( 'footer' );
    }
	/*
	 * 返回菜谱
	 */
	public function getCookbooks() {
		$schoolid = $this->input->post('schoolid');
		$startdate = $this->input->post('startdate');
		$enddate = $this->input->post('enddate');
		$cookbooks=array();
		$this->load->model ( 'cookbook_model' );
		for($start = strtotime($startdate); $start <= strtotime($enddate);$start += 3600*24){
			$date=date ( "Y-m-d", $start);
			$cookbook['date']=$date;
			$breakfast=$this->cookbook_model->find_by_date_type ($schoolid,$date,1);
 	 		$cookbook['breakfast']=$breakfast;
 	 		$lunch=$this->cookbook_model->find_by_date_type ($schoolid,$date,2);
 	 		$cookbook['lunch']=$lunch;
			$dinner=$this->cookbook_model->find_by_date_type ($schoolid,$date,3);
 	 		$cookbook['dinner']=$dinner;
			$cookbooks[]=$cookbook;
 	 	}
		$data ['cookbooks'] = $cookbooks;
		return_json(TRUE,$data,'返回菜谱成功！');
	}
	/*
	 * 添加菜谱商品
	 */
	public function addproduct() {
		$schoolid = $this->input->post('schoolid');
		$date = $this->input->post('date');
		$productid = $this->input->post('productid');
		$type = $this->input->post('type');
		$adminid= $this->session->userdata ( 'adminid' );
		$this->load->model ( 'cookbook_model' );
		$this->load->model ( 'product_model' );
		$temp=$this->cookbook_model->find_by_date_product_type ($schoolid,$date,$productid,$type);
		if(!empty($temp)){
			return_json(FALSE,'','请勿重复添加！');
		}
		$product=$this->product_model->load ($productid);
		$cookbook = array (
			'schoolid' => $schoolid,
			'date' => $date,
			'productid' => $productid,
			'type' => $type,
			'add_time' => date ( "Y-m-d H:i:s" ),
			'adminid' => $adminid,
			'daylim' => $product['daylim'],
			'stock' => $product['daylim']
		);
		$cookbookid=$this->cookbook_model->create ( $cookbook );
		if(!empty($cookbookid)){
			return_json(TRUE,'','添加成功！');
		}else{
			return_json(FALSE,'','添加失败！');
		}
	}
	/*
	 * 删除菜谱商品
	 */
	public function deleteproduct() {
		$id = $this->input->post('id');
		$this->load->model ( 'cookbook_model' );
		$this->cookbook_model->delete ( $id );
		return_json(TRUE,'','删除成功！');
	}
	/*
	 * 修改菜谱商品限购
	 */
	public function updateproduct() {
		$id = $this->input->post('id');
		$daylim = $this->input->post('daylim');
		$this->load->model ( 'cookbook_model' );
		$cookbook = array (
			'daylim' => $daylim,
			'stock' => $daylim
		);
		$this->cookbook_model->update ( $id,$cookbook );
		return_json(TRUE,'','修改成功！');
	}
}
